package com.example.guli.service.edu.controller.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.guli.common.base.result.R;
import com.example.guli.service.edu.entity.Teacher;
import com.example.guli.service.edu.entity.vo.ChapterVo;
import com.example.guli.service.base.DTO.CourseDto;
import com.example.guli.service.edu.entity.Course;
import com.example.guli.service.edu.entity.form.CourseInfoForm;
import com.example.guli.service.edu.entity.vo.WebCourseQueryVo;
import com.example.guli.service.edu.entity.vo.WebCourseVo;
import com.example.guli.service.edu.service.ChapterService;
import com.example.guli.service.edu.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@Api(description="课程")
@RestController
@RequestMapping("/api/edu/course")
public class ApiCourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private ChapterService chapterService;

    @ApiOperation("获取已发布的课程列表")
    @GetMapping("list")
    public R list(
            @ApiParam(value = "查询对象",required = false)
            WebCourseQueryVo webCourseQueryVo
    ){
        List<Course> courseList =courseService.webSelectList(webCourseQueryVo);
        return R.ok().data("data",courseList);
    }

    @ApiOperation("分页获取已发布的课程列表")
    @GetMapping("listPage/{page}/{limit}")
    public R listPage(
            @ApiParam(value = "当前页码",required = true) @PathVariable Long page,
            @ApiParam(value = "每页记录数",required = true) @PathVariable Long limit,
            @ApiParam(value = "查询对象",required = false) WebCourseQueryVo webCourseQueryVo
    ){
        Page<Course> pageParam = new Page<>(page, limit);
        IPage<Course> pageModel=courseService.webSelectPage(pageParam,webCourseQueryVo);
        List<Course> records = pageModel.getRecords();
        long total = pageModel.getTotal();
        long current = pageModel.getCurrent();
        long pages = pageModel.getPages();
        long pageSize = pageModel.getSize();
        return R.ok().data("total",total).data("current",current).data("pageSize",pageSize).data("pages",pages).data("data",records);
    }

    @ApiOperation("根据ID查询课程和课时")
    @GetMapping("get/{courseId}")
    public R getById(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable String courseId){
        //查询课程信息和讲师信息
        WebCourseVo webCourseVo = courseService.selectWebCourseVoById(courseId);
        //查询当前课程的嵌套章节和课时信息
        List<ChapterVo> chapterVoList = chapterService.nestedList(courseId);
        return R.ok().data("course",webCourseVo).data("chapterVoList",chapterVoList);

    }


    @ApiOperation("根据课程id查询课程信息-微服务调用的方法edu")
    @GetMapping("inner/get-course-dto/{courseId}")
    //另一个微服务访问的方法，不直接返回给前端所以不要R对象
    //服务和服务之间的数据传输对象CourseDto
    public CourseDto getCourseDtoById(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable String courseId){
        CourseDto courseDto = courseService.getCourseDtoById(courseId);
        return courseDto;
    }

}
